import { showSuccessToast, showFailToast, showConfirmDialog } from 'vant';

export default {
    install(app) {
        // app == createApp == Vue应用
        let login = async () => {
            //需要登录访问
            let business = app.config.globalProperties.$cookies.get('business') ? app.config.globalProperties.$cookies.get('business') : {}

            let id = business.hasOwnProperty('id') ? business.id : 0;
            let mobile = business.hasOwnProperty('mobile') ? business.mobile : '';

            //调用接口，查询用户是否存在
            var result = await app.config.globalProperties.$POST({
                url: '/business/check',
                params: { id, mobile }
            })

            //查找用户失败
            if (result.code == 1) {
                //如果存在用户，接口会返回最新的用户数据给回我们.覆盖cookie
                app.config.globalProperties.$cookies.set('business', result.data);

                //覆盖全局变量
                app.config.globalProperties.$business.value = result.data

            } else if (result.code == 0) {
                //清除缓存
                app.config.globalProperties.$cookies.remove('business')
            }

            return result
        }

        //在全局应用上面添加了一个自定义的属性
        app.config.globalProperties.$success = showSuccessToast
        app.config.globalProperties.$fail = showFailToast
        app.config.globalProperties.$confirm = showConfirmDialog
        app.config.globalProperties.$login = login
        app.config.globalProperties.$business = app.config.globalProperties.$cookies.get('business') ? ref(app.config.globalProperties.$cookies.get('business')) : ref({})
    }
}
